package com.qzdsoft.erpcloud.controller.com;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.com.SupplierCustomer;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.DepartmentService;
import com.qzdsoft.erpcloud.service.com.StaffService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.SupplierCustomerQueryParam;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.com.SupplierCustomerCatagoryType;
import com.qzdsoft.vo.pu.SupplierCustomerType;

/**
 * 供应商管理
 * @author pc-20170422
 *
 */
@Controller
@RequestMapping("/com/supplier/customer")
public class SupplierProviderController extends BaseController{
	 private static final String  BASEPATH = "com/provider";
	 private static final Logger logger = LoggerFactory.getLogger(SupplierProviderController.class);

	 @Autowired
	private CenterCompanyService actCenterCompanyService;
	 @Autowired
	private CustomerSupplierService customerSupplierService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private StaffService staffService;
	 
	 
	  @Permission(validate=false)
	  @RequestMapping("/provider")
	  public ModelAndView index() {
		  ModelAndView mv = new ModelAndView();
		  mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
		  mv.addObject("typeList", SupplierCustomerType.ALL);
		  mv.addObject("type", SupplierCustomerType.SUPPLIER_CODE);
	      mv.addObject("catagoryList", SupplierCustomerCatagoryType.ALL);
		  mv.setViewName(BASEPATH+"/index");
		  return mv;
	  }


	  /**
       * 获取所有的供应商信息
       * @param request
       * @param response
       * @param page
       * @param rows
       * @return
	 * @throws ParseException 
       */
	  	@ResponseBody
	    @RequestMapping("/provider/data")
	    public LayTableResponse data(HttpServletRequest request, SupplierCustomerQueryParam param) throws ParseException {
	  		logger.debug("客户类型:type={}",param.getType());
	        if(param.isFirst()) {
	        	return  LayTableResponse.nullDataPage();
	        }else {
	        	return customerSupplierService.selectData(param);
	        }
	    }

	  	/**
	  	 * 供应商客户新增
	  	 * @param companyId
	  	 * @param newType
	  	 * @return
	  	 */
	  	@Permission(validate=false)
	  	@RequestMapping("/provider/addinit")
	  	public ModelAndView toAdd(Integer companyId,String newType) {
	  		ModelAndView mv = new ModelAndView();
	  		logger.debug("newType={}"+newType);
	  		//公司
	  		mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//	  		//部门
//	  		mv.addObject("dept", departmentService.getByCompanyId(companyId));
	  		//分类
	  		mv.addObject("catagoryList", SupplierCustomerCatagoryType.ALL);
	  		if(newType.equals(SupplierCustomerType.SUPPLIER_CODE)) {
	  		mv.addObject("type", SupplierCustomerType.SUPPLIER);
	  		}else{
	  		mv.addObject("type", SupplierCustomerType.CUSTOMER);
	  		}
	  		mv.setViewName(BASEPATH+"/add");
	  		return mv;
	  	}


		/**
	  	 * 新增保存
	  	 * @param request
	  	 * @param supplierCustomer
	  	 * @return
	  	 */
	  	 @ResponseBody
	  	 @SysLog(description="供应商新增保存")
	     @RequestMapping("/provider/add")
	     public Object add(HttpServletRequest request, SupplierCustomer supplierCustomer) {
//	         comSupplierCustomer.setType(Integer.parseInt(sessionService.getByKey("comSupplierCustomerType",request).toString()));
//	         comSupplierCustomer.setCompany_id(SessionUtil.getLoggedCompanyId(request, comSupplierCustomer.getCompany_id()));
	  		 logger.debug("供应商管理信息修改:id={}",supplierCustomer.getId());
	        return customerSupplierService.edit(supplierCustomer);
//	  		 return new Response<String>(ResultEnum.SUCCESS);
	     }
	  	
	  	/**
	  	 * 客户供应商编辑
	  	 * @param request
	  	 * @param id
	  	 * @return
	  	 */
	  	@Permission(validate=false)
	     @RequestMapping("/provider/edit")
	     public ModelAndView edit(@RequestParam("id") Integer id) {
	    	 ModelAndView mv = new ModelAndView();
	    	SupplierCustomer customer =  customerSupplierService.selectById(id);
	    	logger.debug("type={}",customer.getType());
	    	if(customer.getType().toString().equals(SupplierCustomerType.SUPPLIER_CODE)) {
		  		mv.addObject("type", SupplierCustomerType.SUPPLIER);
		  		logger.debug("SUPPLIER={}",customer.getType());
	  		}else{
	  		mv.addObject("type", SupplierCustomerType.CUSTOMER);
	  		logger.debug("CUSTOMER={}",customer.getType());
	  		}
	    	//公司
	  		 mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
	    	 mv.addObject("catagoryList", SupplierCustomerCatagoryType.ALL);
	    	 mv.addObject("dept",departmentService.getByCompanyId(customer.getCompanyId()));
	    	 mv.addObject("staff", staffService.getStaffList(customer.getDepartmentId(), customer.getCompanyId()));
	    	 mv.addObject("obj", customer);
	    	 mv.setViewName(BASEPATH+"/edit");
	    	 return mv;
	    	 
	     }
	  	/**
	  	 * 修改保存
	  	 * @param request
	  	 * @param supplierCustomer
	  	 * @return
	  	 */
	  	 @SysLog(description="供应商修改保存")
	  	 @ResponseBody
	     @RequestMapping("/provider/edit_execute")
	     public Object editExecute(HttpServletRequest request, SupplierCustomer supplierCustomer) {
//	         comSupplierCustomer.setType(Integer.parseInt(sessionService.getByKey("comSupplierCustomerType",request).toString()));
//	         comSupplierCustomer.setCompany_id(SessionUtil.getLoggedCompanyId(request, comSupplierCustomer.getCompany_id()));
	  		 logger.debug("供应商管理信息修改:id={}",supplierCustomer.getId());
	        return customerSupplierService.edit(supplierCustomer);
//	  		 return new Response<String>(ResultEnum.SUCCESS);
	     }
	  	 
	  	 
	  	/**
	      * 删除
	      * @param request
	      * @param id
	      * @return
	      */
	  	 @SysLog(description="供应商删除")
	     @ResponseBody
	     @RequestMapping("/provider/del_execute")
	     public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
	    	logger.debug("供应商管理删除操作:id={}",id);
//	    	 return new Response<String>(ResultEnum.SUCCESS);
	    	 return customerSupplierService.del(id);
	     }
	     
	     /**
	      * 供应商客户详情
	      */
	     @Permission(validate=false)
	     @RequestMapping("/provider/view")
	     public ModelAndView view(Integer id) {
	    	 ModelAndView mv = new ModelAndView();
	    	 mv.addObject("obj", customerSupplierService.selectDetailById(id));
	    	 mv.setViewName(BASEPATH+"/view");
	    	 return mv;
	     }
}
